using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponAutomaticGun : Weapon
{
    [Header("武器部件位置")]
    [Tooltip("射击的位置")]public Transform shootPoint;//射线打出的位置
    [Tooltip("子弹特效打出的位置")]public Transform BulletShootPoint;//子弹特效打出的位置
    [Tooltip("子弹壳抛出的位置")]public Transform CasingBulletSpawnPoint;//子弹预制体
    
    
    [Header("枪械属性")]
    [Tooltip("武器射程")]public float range;//武器射程
    [Tooltip("武器射速")]public float fireRate;//武器射速
    //当前武器的每个弹夹的子弹数量
    [Tooltip("当前武器每个弹夹子弹数量")]public int bulletMag;
    [Tooltip("当前子弹数量")]public int currentBulletCount;
    [Tooltip("最大子弹数量")]public int maxBulletCount;
    [Tooltip("子弹预制体")]public GameObject bulletPrefab;
    [Tooltip("子弹壳预制体")]public GameObject casingBulletPrefab;
    // [Tooltip("子弹发射力")]public float bulletForce = 1000f;
    
    [Header("武器状态")]
    [Tooltip("是否自动")]public bool isAutomatic = true;
    [Tooltip("是否装填")]public bool isCharging = false;
    [Tooltip("是否装填完毕")]public bool isCharged = false;
    
    
    
    
    private float originalFireRate;//原始射速
    private float spreadFactor;//射击的一点偏移
    private float fireTimer;//计时器，控制武器射速
    private float bulletForce;//子弹发射的力
    
    // Start is called before the first frame update
    void Start()
    {
        range = 300f;
        maxBulletCount = bulletMag * 5;
        currentBulletCount = bulletMag;
    }

    // Update is called once per frame
    void Update()
    {
        //射速控制
        if (fireTimer < fireRate)
        {
            fireTimer += Time.deltaTime;
        }

        //鼠标左键点击射击
        if (Input.GetMouseButton(0))
        {
            GunFire();
        }
    }

    public override void GunFire()
    {
        if (fireTimer < fireRate || currentBulletCount <= 0) return;
        
        //使用射线检测来射击
        RaycastHit hit;
        Vector3 shootDir = shootPoint.forward;//射线方向
        shootDir += shootPoint.TransformDirection(new Vector3(Random.Range(-spreadFactor, spreadFactor),Random.Range(-spreadFactor, spreadFactor))); //让射击方向偏移，因为不能完全射在同一个点上
        
        if (Physics.Raycast(shootPoint.position, shootDir, out hit, range))
        {
            Debug.Log("射线击中：" + hit.transform.name);
            //绘制红线显示射击方向，红线一定时间后销毁
            Debug.DrawRay(shootPoint.position, shootDir * range, Color.red, 2f);
        }
        currentBulletCount--;//消耗一颗子弹
        fireTimer = 0f;//重置射速计时器
        
        // //子弹预制体的实例化
        // GameObject bullet = Instantiate(bulletPrefab, BulletShootPoint.position, BulletShootPoint.rotation);
        // bullet.GetComponent<Rigidbody>().AddForce(shootDir * bulletForce, ForceMode.Impulse);
        // Destroy(bullet, 5f);//子弹的销毁时间
        //
        // //子弹壳的实例化
        // GameObject casingBullet = Instantiate(casingBulletPrefab, CasingBulletSpawnPoint.position, CasingBulletSpawnPoint.rotation);
        // casingBullet.GetComponent<Rigidbody>().AddForce(shootDir * bulletForce, ForceMode.Impulse);
        // Destroy(casingBullet, 5f);//子弹壳的销毁时间
    }

    public override void Reload()
    {
        throw new System.NotImplementedException();
    }

    public override void ExpaningCrossUpdate(float expanDegree)
    {
        throw new System.NotImplementedException();
    }

    public override void DoReloadAnimation()
    {
        throw new System.NotImplementedException();
    }

    public override void AimIn()
    {
        throw new System.NotImplementedException();
    }

    public override void AimOut()
    {
        throw new System.NotImplementedException();
    }
}
